<?php
class b2c_mdl_business_orders extends dbeav_model{
    var $has_many = array(
        'order_objects'=>'business_orders_objects',
    );

    /**
     * 订单明细
     * @param $order_id
     */
    public function get_order_objects($order_id){
        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,C.bag_unit,C.package_unit,C.unit,C.price,C.cat_id,C.params,C.mktprice,C.p_9,C.p_10 as g_price,(C.price-A.buy_price)*A.has_confirm_num as g_discount FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_business_orders AS B ON A.childOrder_id=B.childOrder_id LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id WHERE A.mainOrder_id='".$order_id."'";
        return $this->db->select($sql);
    }

    public function get_item_main_category($cat_id){
        $sql = "SELECT `parent_id` FROM `sdb_b2c_goods_cat` WHERE `cat_id` = '$cat_id'";
        return $this->db->select($sql);
    }

    /**
     * 供应商订单明细
     * @param $order_id
     */
    public function get_order_items($order_id){
        $sql = "SELECT A.*,C.cost as cost_price FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id WHERE A.childOrder_id='".$order_id."'";
        return $this->db->select($sql);
    }

    /**
     * 供应商订单明细
     * @param $order_id
     */
    public function get_order_items1($order_id){
        $sql = "SELECT A.*,C.cost as cost_price FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id WHERE A.mainOrder_id='".$order_id."'";
        return $this->db->select($sql);
    }

    /**
     * 供应商订单
     * @param $order_id
     */
    public function get_confirm_orders($order_id){
        $sql = "SELECT A.*,B.chinese_all_name,B.english_all_name,D.first_createtime FROM sdb_b2c_business_orders AS A LEFT JOIN sdb_b2c_business_basicmsg AS B ON A.business_id=B.business_id LEFT JOIN (select min(C.createtime) as first_createtime,C.childOrder_id from sdb_b2c_business_orders AS C WHERE C.order_status<>'dead' GROUP BY C.childOrder_id) AS D ON D.childOrder_id=A.childOrder_id WHERE A.mainOrder_id='".$order_id."' and A.order_status<>'dead' order by createtime desc";
        return $this->db->select($sql);
    }

    /**
     * 供应商订单列表供应商、操作员搜索
     * author jyy 2016/10/24
     */
//    public function searchOptions(){
//        $columns = array();
//        foreach($this->_columns() as $k=>$v){
//            if(isset($v['searchtype']) && $v['searchtype']){
//                $columns[$k] = $v['label'];
//            }
//        }
//        /** 添加供应商名称搜索 **/
//        $columns['english_all_name'] = app::get('b2c')->_('供应商名称');
//        //$columns['name'] = app::get('b2c')->_('操作员');
//        /** end **/
//
//        return $columns;
//    }

    //重写过滤条件 by yyj 2015/11/6
    function _filter($filter,$tableAlias=null,$baseWhere=null){
        if (isset($filter) && $filter && is_array($filter) && (array_key_exists('english_all_name', $filter)))
        {
            $obj_business = app::get('b2c')->model('business_basicmsg');
            if($filter['english_all_name']){
                $bb_filter = array(
                    'english_all_name|has'=>$filter['english_all_name'],
                );
                unset($filter['english_all_name']);
            }
            $row_bb = $this->get_finder('*',$bb_filter);
            $business_id = array();
            if ($row_bb){
                foreach ($row_bb as $str_bb){
                    $business_id[] = $str_bb['business_id'];
                }
                $filter['business_id|in'] = $business_id;
            }
        }

        foreach(kernel::servicelist('b2c_mdl_business_orders_objects.filter') as $k=>$obj_filter){
            if(method_exists($obj_filter,'extend_filter')){
                $obj_filter->extend_filter($filter);
            }
        }
        $info_object = kernel::service('sensitive_information');
        if(is_object($info_object)) $info_object->opinfo($filter,'b2c_mdl_business_orders_objects',__FUNCTION__);
        $filter = parent::_filter($filter);
        return $filter;
    }
    /**
     * 重写getList方法
     * author jyy 2016/10/27
     */
//    public function get_finder($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null,&$total=false)
//    {
//        if($_GET['view']==0){
//            $where = ' A.state=\'0\' ';
//        }else{
//            $where = ' A.state=\'1\' ';
//        }
//        $sql = "SELECT A.*,B.member_id,B.order_amount,B.order_num,B.status,B.createtime,D.brand_name,
//                C.unit_num,C.s_price,C.cost as s_cost,(A.buy_price*(1-D.rebate/100))as cost,F.total_confirm_num,
//                G.english_all_name FROM sdb_b2c_business_orders_objects AS A LEFT JOIN sdb_b2c_business_orders AS B
//                ON A.childOrder_id=B.childOrder_id LEFT JOIN sdb_b2c_goods AS C ON A.goods_id=C.goods_id
//                LEFT JOIN sdb_b2c_brand AS D ON C.brand_id=D.brand_id LEFT JOIN (SELECT sum(E.has_confirm_num)
//                AS total_confirm_num,E.goods_id FROM sdb_b2c_business_orders_objects as E GROUP BY E.goods_id) AS F
//                ON A.goods_id = F.goods_id LEFT JOIN sdb_b2c_business_basicmsg AS G ON A.business_id=G.business_id
//                WHERE $where and B.order_status<>'dead' ";
//        //快捷搜索供应商名称---jyy 2016/10/25
//        if($filter && isset($filter['english_all_name'])){
//            $sql .= " and G.english_all_name like '%".$filter['english_all_name'].'%\'';
//        }
//        if($filter && isset($filter['childOrder_id'])){
//            $sql .= " and B.childOrder_id = ".$filter['childOrder_id'];
//        }
//        //高级筛选filter
//        if($filter && isset($filter['business_id'])){
//            $sql .= " and A.business_id = ".$filter['business_id'];
//        }
//        if($filter && isset($filter['mainOrder_id'])){
//            $sql .= " and A.mainOrder_id = ".$filter['mainOrder_id'];
//        }
//        //货品编号
//        if($filter && isset($filter['bn'])){
//            $sql .= " and A.bn = '".$filter['bn'].'\'';
//        }
//        $data = $this->db->selectLimit($sql,$limit,$offset);
//        return $data;
//    }


//    public function get_count(){
//        return count($this->get_finder('*', array(),  0,  -1));
//    }

    /**
     * 重写采购订单导出方法
     * @param array $data
     * @param array $filter
     * @param int $offset
     * @param int $exportType
     */
    public function fgetlist_csv( &$data,$filter,$offset,$exportType =1 ){
        $limit = 100;
        $cols = $this->_columns();
        if(!$data['title']){
            $this->title = array();
            foreach( $this->getTitle($cols) as $titlek => $aTitle ){
                $this->title[$titlek] = $aTitle;
            }
            // service for add title when export
            foreach( kernel::servicelist('export_add_title') as $services ) {
                if ( is_object($services) ) {
                    if ( method_exists($services, 'addTitle') ) {
                        $services->addTitle($this->title);
                    }
                }
            }
            $data['title'] = '"'.implode('","',$this->title).'"';
        }
//error_log(var_export("ssssss",1).PHP_EOL, 3,DATA_DIR."/list.log");
        $list = $this->get_finder(implode(',',array_keys($cols)),$filter,$offset*$limit,$limit);
//error_log(var_export($list,1).PHP_EOL, 3,DATA_DIR."/list.log");
        if(!$list)return false;
        #$data['contents'] = array();
        foreach( $list as $line => $row ){
            // service for add data when export
            foreach( kernel::servicelist('export_add_data') as $services ) {
                if ( is_object($services) ) {
                    if ( method_exists($services, 'addData') ) {
                        $services->addData($row);
                    }
                }
            }
            $rowVal = array();
            foreach( $row as $col => $val ){

                if( in_array( $cols[$col]['type'],array('time','createtime') ) && $val ){
                    $val = date('Y-m-d H:i',$val);
                }

                if( array_key_exists( $col, $this->title ) )
                    $rowVal[] = addslashes(  (is_array($cols[$col]['type'])?$cols[$col]['type'][$val]:$val ) );
            }
            $data['contents'][] = '"'.implode('","',$rowVal).'"';
        }
        return true;

    }

    function getTitle(&$cols){
        $title = array();
        foreach( $cols as $col => $val ){
            if( !$val['deny_export'] )
                $title[$col] = $val['label'].'('.$col.')';
        }
        return $title;
    }

    /**
     * 重写getList方法
     */
    public function get_finder($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null,&$total=false)
    {
        if($_GET['view']==0){
            $where = ' o.state=\'0\' and bo.order_status=\'active\'';
        }else{
            $where = ' o.state=\'1\' and bo.order_status<>\'dead\'';
        }
        $sql = 'SELECT o.obj_id,b.english_all_name,b.operator_id,o.business_id,o.state,u.user_id,u.name,sum(o.quantity) as num
                FROM `sdb_b2c_business_orders_objects` as o
                left join `sdb_b2c_business_basicmsg` as b
                on o.business_id=b.business_id
                left join `sdb_desktop_users` as u
                on b.operator_id=u.user_id
                LEFT JOIN `sdb_b2c_business_orders` AS bo
                ON bo.childOrder_id=o.childOrder_id
                WHERE '.$where.' ';
        //搜索供应商名称---jyy 2016/10/25
        if($filter && isset($filter['english_all_name'])){
            $sql .= " and b.english_all_name like '%".$filter['english_all_name'].'%\'';
        }
        if($filter && isset($filter['name'])){
            $sql .= " and u.name like '%".$filter['name'].'%\'';
        }
        $sql .= " group by o.business_id order by o.obj_id desc ";
        $data = $this->db->selectLimit($sql,$limit,$offset);
        return $data;
    }

    /**
     * 供应商订单列表供应商、操作员搜索
     * author jyy 2016/10/24
     */
    public function searchOptions(){
        $columns = array();
        foreach($this->_columns() as $k=>$v){
            if(isset($v['searchtype']) && $v['searchtype']){
                $columns[$k] = $v['label'];
            }
        }
        /** 添加供应商名称搜索 **/
        $columns['english_all_name'] = app::get('b2c')->_('供应商名称');
        $columns['name'] = app::get('b2c')->_('操作员');
        /** end **/

        return $columns;
    }

    /**
     * 供应商订单列表供应商数量统计count
     * author jyy 2016/10/27
     */
    public function count($filter=null){
        $filter = "1";
        $row = $this->db->select('SELECT count( DISTINCT obj_id) as _count FROM sdb_b2c_business_orders_objects
        WHERE '.$filter);
        return intval($row[0]['_count']);
    }

    public function get_finder_count(){
        return count($this->get_finder('*', array(),  0,  -1));
    }




}